var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../sleep"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const sleep_1 = __importDefault(require("../sleep"));
    const { exec } = require('child_process');
    const netWorkMana = {
        cache: null,
        monitoring: false,
        interrupt: false,
        decodeMessage(outStr) {
            const lines = outStr.replaceAll('\r', '').split('\n').filter(item => item);
            if (lines.length < 3) {
                return null;
            }
            else {
                lines.shift();
            }
            const headerList = lines[0].split(',').map((item) => item.split('Network ')[1]).filter((item) => item);
            const dateList = [];
            const speedList = [];
            for (let i = 1; i < lines.length; i++) {
                const lineStr = lines[i];
                const colItem = lineStr.replaceAll('"', '').split(',');
                const dateStr = colItem.shift();
                const speeds = colItem.map(item => Number(item));
                if (speeds.length === headerList.length) {
                    dateList.push(dateStr);
                    speedList.push(speeds);
                }
            }
            return { headerList, dateList, speedList };
        },
        async getNetworkList(sc = 20, onMessageFunc) {
            return new Promise((resolve) => {
                const command = `chcp 65001 && typeperf -sc ${sc < 20 ? 20 : sc} "\\Network Interface(*)\\Bytes Sent/sec" "\\Network Interface(*)\\Bytes Received/sec`;
                const child = exec(command);
                let outStr = '';
                child.stdout.on('data', (data) => {
                    outStr += data;
                    const mes = this.decodeMessage(outStr);
                    if (mes) {
                        onMessageFunc && onMessageFunc(this.decodeMessage(outStr));
                    }
                    if (this.interrupt) {
                        this.interrupt = false;
                        this.monitoring = false;
                        resolve(mes);
                    }
                });
                child.stderr.on('data', (data) => {
                    console.error(`标准错误: ${data}`);
                });
                child.on('close', (code) => {
                    resolve(this.decodeMessage(outStr));
                });
            });
        },
        async getRecently() {
            if (this.cache) {
                return this.cache;
            }
            else {
                this.openListen();
                await (0, sleep_1.default)(3000);
                return this.cache;
            }
        },
        openListen() {
            if (this.monitoring === false) {
                this.monitoring = true;
                this.getNetworkList(360, ((netWork) => {
                    if (netWork?.dateList?.length < 100) {
                        this.cache = netWork;
                    }
                    else {
                        this.cache = {
                            headerList: netWork.headerList,
                            dateList: netWork.dateList.slice(-100),
                            speedList: netWork.speedList.slice(-100),
                        };
                    }
                }));
            }
        },
        stopListen() {
            if (this.monitoring) {
                this.interrupt = false;
            }
            this.monitoring = false;
        }
    };
    exports.default = netWorkMana;
});
